package br.com.array.android.ma.old;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import br.com.array.android.ma.R;
import br.com.array.android.ma.controller.ItemAlertaAdapter;
import br.com.array.android.ma.model.ItemAlertaModel;
import br.com.array.android.ma.view.FragmentPage;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Almir Gabriel
 * Date: 08/05/2014
 * Time: 10:58
 */
public class EnergiaReativaOld extends FragmentPage {

    private ListView listView;
    private ItemAlertaAdapter adapter;
    private Long tempo = 5000L;
    private EnergiaReativaController controller;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.energia_reativa_main, null);
        listView = (ListView) view.findViewById(R.id.lv_historico);
        final List<ItemAlertaModel> itens = new ArrayList<ItemAlertaModel>();
        controller = new EnergiaReativaController(tempo);
        controller.processar();
        controller.setOnLimiteAtingido(new EnergiaReativaController.OnLimiteAtingido() {
            @Override
            public void onLimiteAtingido() {
                final ItemAlertaModel model = new ItemAlertaModel();
                SimpleDateFormat sdfDate = new SimpleDateFormat("dd/MM/yyyy");
                SimpleDateFormat sdfHora = new SimpleDateFormat("HH:mm");
                Date date = new Date();
                model.setData(sdfDate.format(date));
                model.setHora(sdfHora.format(date));
                model.setTempoDecorrido("100%");
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        itens.add(model);
                        adapter.notifyDataSetChanged();
                    }
                });

            }
        });
        adapter = new ItemAlertaAdapter(getActivity(), itens);
        listView.setAdapter(adapter);

        return view;
    }

    public void setTempo(Long tempo) {
        this.tempo = tempo;
        controller.setTempo(tempo);
    }

    @Override
    public String getTitulo() {
        return "Energia reativa";
    }

}
